<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="品番" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入品番"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="Download" @click="handleExport(2)"
          >导出客户品番信息</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getTable"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tableList"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="品番"
        align="center"
        min-width="130"
        prop="name"
        show-overflow-tooltip
      />
      <el-table-column
        label="箱收容数"
        align="center"
        min-width="100"
        prop="boxQty"
        show-overflow-tooltip
      />
      <el-table-column
        label="箱数/托"
        align="center"
        min-width="100"
        prop="boxTrayQty"
      />
      <el-table-column
        label="托收容数"
        align="center"
        min-width="130"
        prop="trayQty"
      />

      <el-table-column
        label="操作"
        width="140"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getTable"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="品番" prop="name">
          <el-input v-model="form.name" placeholder="请输入品番" />
        </el-form-item>
        <el-form-item label="箱收容数" prop="boxQty">
          <el-input-number
            v-model="form.boxQty"
            :min="1"
            :controls="false"
            style="width: 100%"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="箱数/托" prop="boxTrayQty">
          <el-input-number
            v-model="form.boxTrayQty"
            :min="1"
            :controls="false"
            style="width: 100%"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="托收容数" prop="trayQty">
          <el-input-number
            v-model="form.trayQty"
            :min="1"
            :controls="false"
            style="width: 100%"
            placeholder="请输入"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Post">
// import { listPost, addPost, delPost, getPost, updatePost } from "@/api/system/post";
import pagination from "@/components/Pagination/index.vue";
import RightToolbar from "@/components/RightToolbar/index.vue";
import { nextTick } from "vue";
const props = defineProps({
  // 数据
  customer: {
    type: Object,
    default: undefined,
  },
});
const emit = defineEmits();
const { proxy } = getCurrentInstance();
const tableList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    customerId: props.customer?.id,
  },
  rules: {
    name: [{ required: true, message: "请输入", trigger: "blur" }],
    boxQty: [{ required: true, message: "请输入", trigger: "blur" }],
    boxTrayQty: [{ required: true, message: "请输入", trigger: "blur" }],
    trayQty: [{ required: true, message: "请输入", trigger: "blur" }],
  },
});
const formRef = ref();

const { queryParams, form, rules } = toRefs(data);

async function getTable() {
  loading.value = true;
  try {
    let result = await window.base.tableMaterial({ ...queryParams.value });
    console.log(result,444)
    total.value = result.pagination.total;
    tableList.value = result.data;
  } catch (e) {
  } finally {
    loading.value = false;
  }
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {};

  // console.log(formRef)
  // formRef.value.resetFields();
  // proxy.resetForm("formRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getTable();
}
/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    customerId: props.customer?.id,
  };
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.postId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加品番";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  open.value = true;
  title.value = "修改品番";
  nextTick(() => {
    form.value = { ...row };
  });
}
/** 提交按钮 */
function submitForm() {
  // console.log(formRef.)
  proxy.$refs["formRef"].validate(async (valid) => {
    if (valid) {
      console.log(props.customer);
      form.value.customerId = props.customer?.id;
      if (form.value.id != undefined) {
        let result = await window.base.updateMaterial({ ...form.value });
        if (result.success) {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getTable();
        } else {
          proxy.$modal.msgError(result.msg);
        }
      } else {
        let result = await window.base.createMaterial({ ...form.value });
        if (result.success) {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getTable();
        } else {
          proxy.$modal.msgError(result.msg);
        }
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm("是否确认删除？")
    .then(async function () {
      return window.base.removeMaterial({ ...row });
    })
    .then((result) => {
      if (result.success) {
        proxy.$modal.msgSuccess("删除成功");
        nextTick(() => {
          getTable();
        });
      } else {
        proxy.$modal.msgError(result.msg);
      }
    })
    .catch(() => {});
}
/** 导出按钮操作 */
async function handleExport(type) {
  let paras = {
    type: type,
    query: { ...queryParams.value },
    fileName: type == 1 ? "客户信息.xlsx" : "客户品番信息.xlsx",
  };
  if (type == 2) {
    paras.query.name = undefined;
  }
  const savePath = await window.base.generateExcel(paras);
  if (savePath) {
    proxy.$modal.msgSuccess("导出成功");
  }
}

getTable();
</script>
